# -*- coding: latin-1 -*-

import MySQLdb

from ogame.core import handle
from ogame import queue

class OGBot(object):
    def start(self, db_host, db_user, db_pass, db_name):
        self.db = MySQLdb.connect(db_host, db_user, db_pass, db_name)
        self.cursor = self.db.cursor()
        
        self.handles = {}
        self.queues = {}
        
    def register_player(self, uni, name, pw):
        test = handle.Handle(uni,name,pw)
        try:
            test.connect()
        except BaseException:
            raise BaseException("Wrong username/password")
        del test
        
        self.cursor.execute("SELECT id FROM player WHERE uni = '%s' AND name = '%s'" % (uni,name))
        if self.cursor.fetchone():
            raise BaseException("Player already exists")
        self.cursor.execute("INSERT INTO player (name, pass, uni) VALUES ('%s','%s','%s')" % (name, pw, uni))
        self.db.commit()
        
        self.cursor.execute("SELECT id FROM player WHERE uni = '%s' AND name = '%s'" % (uni,name))
        return self.cursor.fetchone()[0]
    
    def check_login(self, uni, name, pw):
        self.cursor.execute("SELECT id FROM player WHERE uni ='%s' AND name = '%s' AND pass = '%s'" % (uni, name, pw))
        if not self.cursor.fetchone():
            raise BaseException("Wrong username/password/universe")
    
    def get_player_id(self, uni, name):
        self.cursor.execute("SELECT id FROM player WHERE uni ='%s' AND name = '%s'" % (uni, name))
        player_id = self.cursor.fetchone()
        if not player_id:
            raise BaseException("No such player")
        return player_id[0]
    
    def get_player_data(self, player_id):
        self.cursor.execute("SELECT uni, name, pass FROM player WHERE id = %d" % (player_id))
        data = self.cursor.fetchone()
        if not data:
            raise BaseException("No such player")
        return data
        
    def dump_techtree(self,player_id):
        techtree = self.handles[player_id].fetchTechtree()
        sql = "INSERT INTO techtree (id, name) VALUES "
        for id, name in techtree.items():
            sql += "(%d, '%s'), " % (id, name)
        sql = sql[:-2]
        self.cursor.execute(sql)
        self.db.commit()
        
    def get_tech_name(self,tech_id):
        self.cursor.execute("SELECT name FROM techtree WHERE id = %d" % tech_id)
        return self.cursor.fetchone()[0].decode("latin-1")
        
    def get_tech_id(self,tech_name):
        self.cursor.execute("SELECT id FROM techtree WHERE name = '%s'" % tech_name)
        return self.cursor.fetchone()[0]
    
    def create_handle(self, player_id):
        if player_id in self.handles:
            raise BaseException("Handle already exists")
        data = self.get_player_data(player_id)
        self.handles.update({ player_id: handle.Handle(data[0],data[1],data[2]) })
        return player_id
    
    def create_queue(self, player_id):
        if player_id in self.queues:
            raise BaseException("Queue already exists")
        self.queues.update({ player_id: queue.Queue(self, player_id) })
        return player_id
    